import { Body, Controller, Get, Inject, Post, Query } from "@nestjs/common";
import { CreateLog, Roles } from "@/common/decorators";
import { Role } from "@/common/enum/role.enum";
import { RequiredPipe } from "@/common/pipes/required.pipe";
import { SysCascaderItem } from "@/database/entity/SysCascaderItem";
import { CascaderItemService } from "./csdr-item.service";
import { CreateCascaderItemDto } from "./dto/create-csdr-item.dto";
import { UpdateCascaderItemDto } from "./dto/update-csdr-item.dto";

@Controller("sys/cascaderItem")
export class CascaderItemController {
    constructor(@Inject() private readonly cascaderItemService: CascaderItemService) {}

    /** 新增级联字典项 */
    @CreateLog({ module: "系统字典", type: "create", action: "创建级联字典项" }, SysCascaderItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("create")
    async create(@Body() data: CreateCascaderItemDto) {
        return this.cascaderItemService.create(data);
    }

    /** 删除级联字典项 */
    @CreateLog({ module: "系统字典", type: "delete", action: "删除级联字典项" }, SysCascaderItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("remove")
    async remove(@Body("code", RequiredPipe) code: string, @Body("typeId", RequiredPipe) typeId: string) {
        return this.cascaderItemService.remove(code, typeId);
    }

    /** 更新级联字典项 */
    @CreateLog({ module: "系统字典", type: "update", action: "更新级联字典项" }, SysCascaderItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("update")
    async update(@Body() data: UpdateCascaderItemDto) {
        return this.cascaderItemService.update(data);
    }

    /** 查看详情 */
    @Get("findById")
    async findById(@Query("id", RequiredPipe) id: string) {
        return this.cascaderItemService.findById(id);
    }

    /** 根据code查询路径 */
    @Get("findPathByCode")
    async findPathByCode(@Query("code", RequiredPipe) code: string) {
        return this.cascaderItemService.findPathByCode(code);
    }

    /** 获取级联选项 */
    @Get("listOptions")
    async listOptions(@Query("typeCode", RequiredPipe) typeCode: string) {
        return this.cascaderItemService.listOptions(typeCode);
    }

    /** 查询树形列表 */
    @Get("listTree")
    async treeList(@Query("typeCode", RequiredPipe) typeCode: string) {
        return this.cascaderItemService.listTree(typeCode);
    }
}
